public class BinarySearchTree {
    public static class Node {
        int val;
        Node left;
        Node right;
        public Node(int val) {
            this.val = val;
        }
    }
    public Node root;//根节点

    /**
     * 查找
     * @param key
     * @return
     */
    public Node search(int key) {
        if (this.root == null) {
            return null;
        }
        Node cur = this.root;
        while (cur != null) {
            if (cur.val == key) {
                return cur;
            }else if (cur.val > key) {
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }
        return null;
    }

    /**
     * 插入元素
     * @param key
     * @return
     */
    public boolean insert(int key) {
        Node node = new Node(key);
        if (this.root == null) {
            this.root = node;
            return true;
        }
        Node parent = null;
        Node cur = this.root;
        while (cur != null) {
            if (cur.val == key) {
                return false;
            }if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        if (parent.val > key) {
            parent.left = node;
        }else{
            parent.right = node;
        }
        return true;
    }

    /**
     * 删除元素
     * @param key
     * @return
     */
    public boolean remove(int key) {
        if (this.root == null) {
            return false;
        }
        //找要删除的元素
        Node cur = this.root;
        Node parent = null;
        while (cur != null) {
            if (cur.val == key) {
                removeKey(parent,cur);
                return true;
            }else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else {
                parent = cur;
                cur = cur.right;
            }
        }
        return false;
    }
    public void removeKey(Node parent,Node cur) {
        if (cur.left == null) {
            if (cur == this.root) {
                this.root = cur.right;
            }else if (cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if (cur.right == null) {
            if (cur == this.root) {
                this.root = cur.left;
            }else if (cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            Node targetParent =  cur;
            Node target = cur.right;
            //从要删除的节点右树的最左边那个节点替换
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(target == targetParent.left) {
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }
}
